package com.cctc.juc.demo.Bitc.Tmp.ADemo;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

public class GCountDownLatchRunDemo {

    // 效果 6： 等到 A、B、C 全部执行完成之后，D 再执行。

    private static final CountDownLatch countDownLatch = new CountDownLatch(3);

    public static void main(String[] args) {
        Thread t4 = new Thread(() -> {
            System.out.println("D 自我阻塞，等待 A、B、C 执行...");
            try {
                countDownLatch.await();
                System.out.println("D");
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        });

        Thread t1 = new Thread(() -> {
            System.out.println("A");
            try {
                TimeUnit.SECONDS.sleep(3);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            countDownLatch.countDown();
        });

        Thread t2 = new Thread(() -> {
            System.out.println("B");
            try {
                TimeUnit.SECONDS.sleep(3);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            countDownLatch.countDown();
        });

        Thread t3 = new Thread(() -> {
            System.out.println("C");
            try {
                TimeUnit.SECONDS.sleep(3);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            countDownLatch.countDown();
        });

        t4.start();
        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }

        t1.start();
        t2.start();
        t3.start();

    }

}
